/*
 * @Author: TANGB02 13233582+Ace-Tang@user.noreply.gitee.com
 * @Date: 2023-12-19 15:54:46
 * @LastEditors: TANGB02 13233582+Ace-Tang@user.noreply.gitee.com
 * @LastEditTime: 2024-03-08 16:13:54
 * @FilePath: \nmhv1.0\nmhv1.0\src\request\request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "axios";
import LocalCatch from "./token.js";
import vRouter from "@/router.config.js";
import { message } from "ant-design-vue";
import token from "./token.js";

const router = vRouter;
export const http = axios.create({
  baseURL: "/api",
});
// 拦截器
http.interceptors.request.use(
  (config) => {
    const token = LocalCatch.getToken("token");
    console.log(token);

    if (token) {
      config.headers.Authorization = token;
      if (!config.headers.Authorization) {
        console.log("token未绑定");
      }
    }

    return config;
  },
  (err) => {
    console.log(err);
  }
);
http.interceptors.response.use(
  (res) => {
    return res ? res.data : res;
  },
  (err) => {
    console.log("err");
    console.log(err);

    //无权限
    if (err.response.status === 401) {
      setIsLogin(false);
      message.error("请先登录");

      //返回登录界面
      setTimeout(() => {
        router.push({ name: "home" });
      }, 1000);
    } else if (err.response.status === 403) {
      // token过期
      message.error("登录过期,请重新登录！");
      setIsLogin(false);
    }
  }
);
